package com.app.components;

import android.app.Activity;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.activity.R;

public class TopMenu {

	private Button btnLeft;
	private TextView tvTitle;

	private OnClickListener leftClick; // left button event

	private String leftBtnText;// left button text

	private int leftImgResId; // left button default background

	private int leftImgActiveResId;// when press left button,

	private String menuTitle;// 标题文字

	private Activity act;

	private boolean isLeftButtonDisable;
	
	private int btnWidth;

	public TopMenu(Activity act, String leftBtnText, OnClickListener leftClick,
			int leftImgResId, int leftImgActiveResId, String menutitle) {
		this.act = act;
		this.leftBtnText = leftBtnText;
		this.leftClick = leftClick;
		this.leftImgResId = leftImgResId;
		this.leftImgActiveResId = leftImgActiveResId;
		this.menuTitle = menutitle;
	}
	public TopMenu(Activity act, String leftBtnText, OnClickListener leftClick,
			int leftImgResId, int leftImgActiveResId, String menutitle,int btnWidth) {
		this.act = act;
		this.leftBtnText = leftBtnText;
		this.leftClick = leftClick;
		this.leftImgResId = leftImgResId;
		this.leftImgActiveResId = leftImgActiveResId;
		this.menuTitle = menutitle;
		this.btnWidth=btnWidth;
	}


	public TopMenu(Activity act, String menutitle, boolean isleftButtonDisable) {
		this.act = act;
		this.menuTitle = menutitle;
		this.isLeftButtonDisable = isleftButtonDisable;
	}

	// 左侧按钮返回事件
	private OnClickListener leftButtonOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			act.finish();
		}
	};

	// 按钮触摸改变状态
	public OnTouchListener changeState(final View view) {
		return new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					view.setBackgroundResource(R.drawable.top_button_selected);

				} else if (event.getAction() == MotionEvent.ACTION_UP) {

					view.setBackgroundResource(R.drawable.top_button_unselect);

				}
				return false;
			}

		};
	}

	public void onCreate() {
		btnLeft = (Button) act.findViewById(R.id.btnTopMenuLeft);
		tvTitle = (TextView) act.findViewById(R.id.tvTitle);
		btnLeft.setOnClickListener(leftClick);
		btnLeft.setBackgroundResource(leftImgResId);

		btnLeft.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					btnLeft.setBackgroundResource(leftImgActiveResId);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					btnLeft.setBackgroundResource(leftImgResId);
				}
				return false;
			}

		});

		if (isLeftButtonDisable) {
			btnLeft.setVisibility(View.GONE);
		}

		btnLeft.setText("  " + leftBtnText);
		if(btnWidth==0){
			btnWidth=60;
		}
		RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) btnLeft.getLayoutParams(); 
		int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
						btnWidth, act.getResources().getDisplayMetrics());
		linearParams.width=px;
		tvTitle.setText(menuTitle);
	}
 
}
